/*

This file is part of roomyak.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
roomyak is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
roomyak is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with roomyak; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/

/*
 * gnome compile flags
 *
 * CFLAGS  += $(shell pkg-config --cflags libgnomeui-2.0)
 * LDFLAGS += $(shell pkg-config --libs   libgnomeui-2.0)
 *
 *
 * jni compile flags
 *
 * CFLAGS  += -I${JAVA_HOME}/include/
 * CFLAGS  += -I${JAVA_HOME}/include/linux
 *
 */

#include <jni.h>

/* following header automatically generated via
 * shell$> javah com.datasentinel.util.gnome.GTKThumbnailer
 */

#include "com_datasentinel_util_gnome_GTKThumbnailer.h"

#include <libgnomevfs/gnome-vfs.h>
#include <libgnomeui/gnome-thumbnail.h>

JNIEXPORT jbyteArray JNICALL
Java_com_datasentinel_util_gnome_GTKThumbnailer_thumbnail (JNIEnv * _env,
							   jobject _obj,
							   jstring _str,
							   jboolean _large)
{
  gsize buf_sz = -1;
  gchar *buffer = NULL;
  jbyteArray ans = NULL;
  char *thumb_uri = NULL;
  char *filename = NULL;
  const gchar *non_canonical_filename = NULL;

  GError *error = NULL;
  GnomeVFSFileInfo *vfsFileInfo = NULL;
  GnomeThumbnailFactory *factory = NULL;
  GdkPixbuf *pixBuf = NULL;

  non_canonical_filename = (*_env)->GetStringUTFChars (_env, _str, 0);

  if (non_canonical_filename)
  {
    if (!g_thread_supported ())
      g_thread_init (NULL);
    gnome_vfs_init ();
    filename = gnome_vfs_make_uri_from_shell_arg (non_canonical_filename);
    vfsFileInfo = gnome_vfs_file_info_new ();
    if (filename)
    {
      gnome_vfs_get_file_info (filename, vfsFileInfo,
			       GNOME_VFS_FILE_INFO_DEFAULT |
			       GNOME_VFS_FILE_INFO_GET_MIME_TYPE);

      factory =
	gnome_thumbnail_factory_new (JNI_TRUE ==
				     _large ? GNOME_THUMBNAIL_SIZE_LARGE :
				     GNOME_THUMBNAIL_SIZE_NORMAL);

      thumb_uri =
	gnome_thumbnail_factory_lookup (factory, filename,
					vfsFileInfo->mtime);

      if (thumb_uri)
      {
	pixBuf = gdk_pixbuf_new_from_file (thumb_uri, &error);
	g_free (thumb_uri);
      }

      if (!pixBuf)
      {
	if (gnome_thumbnail_factory_can_thumbnail
	    (factory, filename, vfsFileInfo->mime_type, vfsFileInfo->mtime))
	{
	  pixBuf =
	    gnome_thumbnail_factory_generate_thumbnail (factory,
							filename,
							vfsFileInfo->
							mime_type);

	  if (pixBuf)
	  {
	    gnome_thumbnail_factory_save_thumbnail (factory, pixBuf,
						    filename,
						    vfsFileInfo->mtime);
	  }
	  else
	  {
	    gnome_thumbnail_factory_create_failed_thumbnail (factory,
							     filename,
							     vfsFileInfo->
							     mtime);
	  }
	}
      }

      if (pixBuf)
      {
	gdk_pixbuf_save_to_buffer (pixBuf, &buffer, &buf_sz, "png",
				   &error, (char *) NULL);
	if (buffer)
	{
	  ans = (*_env)->NewByteArray (_env, buf_sz);
	  (*_env)->SetByteArrayRegion (_env, ans, 0, buf_sz,
				       (jbyte *) buffer);
	  g_free (buffer);
	}
	g_object_unref (pixBuf);
      }
      g_object_unref (factory);
      g_free (filename);
    }
    gnome_vfs_file_info_unref (vfsFileInfo);
    (*_env)->ReleaseStringUTFChars (_env, _str, non_canonical_filename);

    if(error)
    {
      printf("error: %s\n",error->message);
      g_free(error);
    }
  }

  return (ans);
}
